package com.gxa.jjty.phy.controller;

import com.gxa.jjty.core.common.JsonResult;
import com.gxa.jjty.core.pojo.Evaluate;
import com.gxa.jjty.core.pojo.MyEvaluateVo;
import com.gxa.jjty.core.pojo.Torder;
import com.gxa.jjty.core.util.StringUtil;
import com.gxa.jjty.phy.service.EvaluateService;
import com.gxa.jjty.phy.service.NewsService;
import com.gxa.jjty.phy.service.ToderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * @ClassName EvaluateController
 * @Author: ws
 * @date 2021/12/8 10:06
 */
@RestController
@RequestMapping("/evaluate")
@Api(tags = "评价前台接口")
@Validated
public class EvaluateController {

    @Autowired
    EvaluateService evaluateService;

    @Autowired
    NewsService newsService;

    @Autowired
    ToderService toderService;

    @PostMapping("/insert")
    @ApiOperation("提交评价操作(生成对应评价的记录)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单的id", paramType = "query", dataType = "int", required = true),
            @ApiImplicitParam(name = "score", value = "评分", paramType = "query", dataType = "String", required = true),
            @ApiImplicitParam(name = "serverTime", value = "服务时长", paramType = "query", dataType = "String", required = false),
            @ApiImplicitParam(name = "serverAttitude", value = "服务态度", paramType = "query", dataType = "String", required = false),
            @ApiImplicitParam(name = "remark", value = "留言、备注", paramType = "query", dataType = "String", required = false),
            @ApiImplicitParam(name = "imgUrls", value = "图片路径数组", dataType = "String", required = false),
    })
    public Map insert(@ApiIgnore  Evaluate evaluate, String imgUrls) throws IllegalAccessException {

        System.out.println(imgUrls);
        System.out.println(evaluate);

        evaluate.setEvaluateTime(new Timestamp(System.currentTimeMillis()));
        Evaluate insert = evaluateService.insert(evaluate);

        if (!StringUtil.isEmpty(imgUrls)) {

            evaluateService.insertImg(evaluate.getOrderId(), imgUrls);
        }

        Torder torder = new Torder();
        torder.setOrderId(evaluate.getOrderId());
        List<Torder> select = toderService.select(torder);
        Torder torder1 = select.get(0);

        // 修改订单表中的flag是否被评论的状态(2: 陪护员待评论的)
        torder1.setFlag(2);
        toderService.updateById(torder1);

        // 向陪护员发送提示信息
        newsService.insert(torder1.getPhyId(), "您的订单已评价！", "您的订单已评价，请查看");
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(insert);

        return jsonResult.getDataMap();
    }

    @GetMapping("/getMyEvaluate")
    @ApiOperation("通过陪护id获取待评价信息")
    public Map getMyEvaluate(@RequestParam("phyId") Long phyId){

        List<MyEvaluateVo> myEvaluate = evaluateService.getMyEvaluate(phyId);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(myEvaluate);
        jsonResult.put("total",myEvaluate.size());

        return jsonResult.getDataMap();
    }

}
